package com.scuthnweb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.scuthnweb.dao.Album_picDao;
import com.scuthnweb.domain.Album;
import com.scuthnweb.domain.Album_pic;

public class Album_picDaoImpl extends HibernateDaoSupport implements Album_picDao{

	@Override
	public Album_pic get(Integer pid) {
		return (Album_pic) this.getHibernateTemplate().get(Album_pic.class, pid);
	}

	@Override
	public List findByAid(Integer aid) {
		String hql = "FROM Album_pic a WHERE a.album.id=?";
		List ls = this.getHibernateTemplate().find(hql, new Object[]{aid});
	    return ls;
	}

	@Override
	public void create(Album_pic album_pic) {
		this.getHibernateTemplate().save(album_pic);
	}

	@Override
	public void update(final Album_pic album_pic) {
		this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
						session.setFlushMode(FlushMode.AUTO); 
						session.update(album_pic); 
						session.flush(); 
						return null; 
					}
		});		
	}

	@Override
	public void delete(final Album_pic album_pic) {
		this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session) throws HibernateException, SQLException {
						session.setFlushMode(FlushMode.AUTO);
						session.beginTransaction();
						Query query = session.createSQLQuery("DELETE FROM album_pic WHERE id=?")
								.setInteger(0, album_pic.getId() );
						query.executeUpdate();					
						session.getTransaction().commit();
						return null; 
					}
		});			
	}

}
